#!/bin/bash
function help {
  echo "Error : "
  echo "    qspy <id> stdout"
  echo "  or"
  echo "    qspy <id> stderr"
  echo ""
}

ID=$1
FILE=$2
EXT="OU"
if [ "$FILE" = "stderr" ]; then
  EXT="ER"
else
  if [ "$FILE" = "stdout" ]; then
    EXT="OU"
  else
    help
    exit 0
  fi
fi
ID=`qstat -f $ID | grep "Job Id" | awk -F ': ' '{print $2}'`
MY_HOST=`qstat -f $ID | grep exec_host | awk -F '= ' '{print $2}' | awk -F '/' '{print $1}'`


CMD="$MY_HOST cat /var/spool/torque/spool/$ID.$EXT"
echo "Job is running on $MY_HOST"
echo "$ID.$EXT "
ssh $CMD
